package farmacia.gui.cadastro.estoque;



import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import farmacia.dao.SubstanciaDao;
import farmacia.entidade.estoque.Substancia;
import farmacia.valida.CustomPlainDocument;
import farmacia.valida.FormataTexto;
import farmacia.valida.ValidaCampo;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.UIManager;
import javax.swing.JComboBox;

public class JfSubstancia extends JFrame {
	SubstanciaDao listaTodos;
	ArrayList <Substancia> listaSubstancia ;
	
	private JPanel contentPane;
	private JTextField textField;
	private JTextField tfSubstancia;
	
	private JTextField textField_1;
	private JComboBox cbSubstancia;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JfSubstancia frame = new JfSubstancia();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JfSubstancia() {
	
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(0, 0, 436, 136);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblCod = new JLabel("Cod");
		lblCod.setBounds(12, 12, 70, 14);
		panel_1.add(lblCod);
		
		textField = new JTextField(20);
		
		textField.setEditable(false);
		textField.setBounds(12, 26, 70, 18);
		panel_1.add(textField);
		textField.setColumns(10);
		
		tfSubstancia = new JTextField(new CustomPlainDocument(), "", 5);
		
		tfSubstancia.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
			tfSubstancia.setText(tfSubstancia.getText().toUpperCase());
				
			
				
				
			
			}
		});
		tfSubstancia.setBounds(94, 26, 330, 18);
		panel_1.add(tfSubstancia);
		tfSubstancia.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("Nome");
		lblNewLabel.setBounds(98, 12, 70, 14);
		panel_1.add(lblNewLabel);
		
		JButton btnInserir = new JButton("Inserir");
		btnInserir.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				try {
					salvarSubstancia();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(rootPane, "Nenhum substancia cadastrada" + e1);
				}
			}
		});
		btnInserir.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				try {
					salvarSubstancia();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(rootPane, "Nenhum cliente cadastrado1" + e1);
				}
			
			}
		});
		btnInserir.setBounds(12, 100, 86, 24);
		panel_1.add(btnInserir);
		
		JButton btnAlterar = new JButton("Alterar");
		
		btnAlterar.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
			   
			}
		});
		
		
		btnAlterar.setBounds(120, 100, 86, 24);
		panel_1.add(btnAlterar);
		
		JButton btnExcluir = new JButton("Excluir");
		
		/*btnExcluir.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				                      
			        try {
			            excluirCliente();
			            listarCliente();
			        } catch (SQLException ex) {
			           JOptionPane.showMessageDialog(rootPane, "Erro no botao excluir"+ex);
			     
			    }                          

			}
		});*/
		btnExcluir.setBounds(228, 100, 86, 24);
		panel_1.add(btnExcluir);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.setBounds(338, 100, 86, 24);
		panel_1.add(btnSalvar);
		
	 cbSubstancia = new JComboBox();
	 cbSubstancia.addMouseListener(new MouseAdapter() {
	 	@Override
	 	public void mouseReleased(MouseEvent e) {
	 		carregaCombo();
	 	}
	 });
		
		
		cbSubstancia.setBounds(94, 65, 330, 23);
		panel_1.add(cbSubstancia);
		
		JLabel lblNewLabel_1 = new JLabel("Substancia");
		lblNewLabel_1.setBounds(98, 48, 108, 14);
		panel_1.add(lblNewLabel_1);
		
		textField_1 = new JTextField();
		textField_1.setBounds(12, 70, 70, 18);
		panel_1.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblCod_1 = new JLabel("Cod");
		lblCod_1.setBounds(12, 48, 70, 14);
		panel_1.add(lblCod_1);
	}
	private void salvarSubstancia() throws SQLException{
		Substancia sub = new Substancia();
		
		sub.setSubstancia(this.tfSubstancia.getText());
		
		SubstanciaDao dao = new SubstanciaDao();
		dao.adicionar(sub);
		
		
	}
	private void alterarSubstancia()
	{
	   Substancia sub = new Substancia();
	   sub.setSubstancia(this.tfSubstancia.getText());
	}
	/*private void excluirSubstancia() {                           
		int resp = JOptionPane.showConfirmDialog(this, "Deseja realmente excluir este cliente? ",
                "Confirmação", JOptionPane.YES_NO_OPTION);
        if (resp == JOptionPane.YES_NO_OPTION) {
            SubstanciaDao dao = new SubstanciaDao();
            dao.excluir(substancia.get(jTTabela.getSelectedRow()));
             mostrarPesquisa(substancia);
        }*/
	public boolean validaCampo()
	{
		if(!tfSubstancia.getText().equals("")){
			return true;
		}else{
				  JOptionPane.showMessageDialog(rootPane, "Existem campos em branco");
			}
		return false;
		}
	
	public void carregaCombo() throws SQLException
	{
		
		SubstanciaDao dao;  
        try {  
            dao = new SubstanciaDao();  
            List<Substancia> minhalista = dao.getLista();  
            for (int i = 0; i < minhalista.size(); i++) {  
                cbSubstancia.addItem(minhalista.get(i).getSubstancia());  
            }  
            cbSubstancia = new JComboBox();  
        } catch (SQLException ex) {  
            JOptionPane.showMessageDialog(null,"Erro no CbxCadastro ! " + ex);  
        }  
	  
	    }  
	
	
}
